#include <iostream>
#include <string>
#include <cstdlib>
#include <unistd.h>

using namespace std;

bool GetQueryString(string& query)
{
    string method = getenv("METHOD");
    if (method == "GET")
    {
        query = getenv("QUERY_STRING");
        return true;
    }
    else if (method == "POST")
    {
        int content_length = atoi(getenv("CONTENT_LENGTH"));
        char ch = 0;
        while (content_length != 0)
        {
            read(0, &ch, 1);
            query.push_back(ch);
            content_length--;
        }
        return true;
    }
    else
    {
        return false;
    }
}

void CutString(string& in, string& left, string& right, string& op, const string& sep)
{
    int pos1 = in.find(sep);
    int pos2 = in.rfind(sep);
    if (pos1 != string::npos && pos2 != string::npos)
    {
        left = in.substr(0, pos1);
        right = in.substr(pos1 + sep.size(), pos2 - pos1 - 1);
        op = in.substr(pos2 + sep.size());
    }
}

int my_calculate(int x, int y, string op)
{
    if (op == "add") return x + y;
        
    if (op == "sub") return x - y;
        
    if (op == "mul") return x * y;
        
    if (op == "div") return x / y;

    return -1;        
}

int main()
{
    string query_string;
    GetQueryString(query_string);
    
    string str_x, str_y, str_op;
    CutString(query_string, str_x, str_y, str_op, "&");

    string x_name, x_val, tmp_x;
    CutString(str_x, x_name, x_val, tmp_x, "=");

    string y_name, y_val, tmp_y;
    CutString(str_y, y_name, y_val, tmp_y, "=");
    
    string op_name, op_val, tmp_op;
    CutString(str_op, op_name, op_val, tmp_op, "=");

    int x = stoi(x_val);
    int y = stoi(y_val);
    int result = my_calculate(x, y, op_val);
    
    cout << "<!DOCTYPE html>";
    cout << "<html>";
    cout << "<head>";
    cout << "<meta charset=\"utf-8\">";
    cout << "<title>计算结果</title>";
    cout << "</head>";
    cout << "<body style=\"font-family: Arial, sans-serif; background-color: #f5f5f5;\">";
    cout << "<div style=\"width: 50%; margin: 50px auto; background-color: #fff; padding: 20px; border-radius: 5px;\">";
    cout << "<h1 style=\"color: #333; text-align: center;\">计算结果:</h1>";
    cout << "<h2 style=\"color: #007bff; text-align: center;\">" << x << " " << op_val << " " << y << " = " << result << "</h2>";
    cout << "</div>";
    cout << "</body>";
    cout << "</html>";
    return 0;
}











